package cn.alone.user.service.impl;
import cn.alone.basic.BusinessException;
import cn.alone.basic.constant.BaseConstants;
import cn.alone.basic.service.impl.BaseServiceImpl;
import cn.alone.basic.util.MD5Utils;
import cn.alone.basic.util.StrUtils;
import cn.alone.org.mapper.EmployeeMapper;
import cn.alone.user.domain.Logininfo;
import cn.alone.user.domain.User;
import cn.alone.user.dto.UserDto;
import cn.alone.user.mapper.LogininfoMapper;
import cn.alone.user.mapper.UserMapper;
import cn.alone.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LogininfoMapper logininfoMapper;

    /**
     * 用户手机注册
     * @param dto
     */
    @Override
    public void phoneRegister(UserDto dto) {
         /*1.空校验*/
        if(dto.getPassword().length()<=0
                ||dto.getPhone().length()<=0
                ||dto.getConfirmPassword().length()<=0
                ||dto.getVerifyCode().length()<=0){
            throw new BusinessException("参数错误，请完整输入不要留空！！");
        }
        /*密码是否一致*/
        if (!dto.getConfirmPassword().equals(dto.getPassword())){
            throw new BusinessException("两次密码不一致");
        }
        /*3.用户是否被注册*/
        User userTmp = userMapper.loadByPhone(dto.getPhone());
        if (userTmp!=null){
            throw new BusinessException("用户名已存在，请直接登录");
        }

        /*1.验证码是否过期 使用redis固定前缀+手机号获取数据*/
        Object o = redisTemplate.opsForValue().get(BaseConstants.BUSINESS_REGISTER_PREFIX + dto.getPhone());
        if(o==null){
            throw new BusinessException("验证码已过期,请重新获取");
        }
        /*2.对比验证码是否正确*/
        String code = ((String)o).split(":")[0];
        /*对比验证码不区分大小写*/
        if(!dto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码输入错误,重新输入");
        }
        /*三:存数据,克隆数据保存到数据库*/
        User user = UserDto2user(dto);
        Logininfo logininfo = user2logininfo(user);
        /*1.保存logininfo*/
        logininfoMapper.save(logininfo);
        /*2.保存 user中的Logininfo_id 和user数据*/
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }


    /**
     *同名原则拷贝
     * @param dto
     * @return
     */
    private User UserDto2user(UserDto dto) {
        User user = new User();
        user.setUsername(dto.getPhone());
        user.setPhone(dto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        //使用盐值加密
        String md5Pwd = MD5Utils.encrypByMd5(dto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

    /**
     * 同名原则拷贝
     * @param user
     * @return
     */
    private Logininfo user2logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }
}
